package com.example.shirodemo.handler;

import com.example.core.libs.JSONResult;
import com.example.shirodemo.properties.ShiroProperties;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)//提高优先级
public class GlobalExceptionHandler {
    @Autowired
    ShiroProperties shiroProperties;

    @ExceptionHandler(value = AuthorizationException.class)
    public void handleAuthorizationException(AuthorizationException authorizationException, HttpServletResponse response) throws IOException {
        response.sendRedirect(shiroProperties.getUnauthorizedUrl());
    }
}
